package planit.view;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import planit.controller.ControllerFactory;
import planit.controller.ControllerFactory.ControllerType;
import planit.model.Model;
import planit.model.Person;
import planit.view.wrappers.ModelCellRenderer;
import planit.view.wrappers.PersonWrapper;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class EditPersonDialog extends javax.swing.JDialog {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel selectLabel;
	private JList personList;
	private JTextField nameTextField;
	private JLabel emailLabel;
	private AbstractAction cancelAction;
	private JScrollPane jScrollPane1;
	private JButton saveButton;
	private JButton cancelButton;
	private JTextField telephoneTextField;
	private JLabel telephoneLabel;
	private JTextField emailTextField;
	private JLabel nameLabel;
	private Model model;

	public EditPersonDialog(Model model) {
		super();
		this.model = model;
		initGUI();
		updatePersonList();
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					selectLabel = new JLabel();
					getContentPane().add(selectLabel);
					selectLabel.setText("Select a Person");
					selectLabel.setBounds(12, 12, 91, 16);
				}
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(12, 34, 79, 184);
					{
						ListModel personListModel = new DefaultComboBoxModel(
								new String[] { "Item One", "Item Two" });
						personList = new JList();
						jScrollPane1.setViewportView(personList);
						personList.setModel(personListModel);
						personList.setBounds(12, 34, 79, 184);
						personList
								.addListSelectionListener(new ListSelectionListener() {
									public void valueChanged(
											ListSelectionEvent evt) {
										personListValueChanged(evt);
									}
								});
					}
				}
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Name:");
					nameLabel.setBounds(97, 34, 94, 16);
				}
				{
					nameTextField = new JTextField();
					getContentPane().add(nameTextField);
					nameTextField.setBounds(97, 50, 177, 23);
				}
				{
					emailLabel = new JLabel();
					getContentPane().add(emailLabel);
					emailLabel.setText("E-mail Address:");
					emailLabel.setBounds(97, 79, 122, 16);
				}
				{
					emailTextField = new JTextField();
					getContentPane().add(emailTextField);
					emailTextField.setBounds(97, 95, 177, 23);
				}
				{
					telephoneLabel = new JLabel();
					getContentPane().add(telephoneLabel);
					telephoneLabel.setText("Telephone Number:");
					telephoneLabel.setBounds(97, 124, 137, 16);
				}
				{
					telephoneTextField = new JTextField();
					getContentPane().add(telephoneTextField);
					telephoneTextField.setBounds(97, 140, 177, 23);
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					cancelButton.setText("Cancel");
					cancelButton.setBounds(196, 233, 70, 23);
					cancelButton.setAction(getCancelAction());
				}
				{
					saveButton = new JButton();
					getContentPane().add(saveButton);
					saveButton.setAction(ControllerFactory
							.getController(ControllerType.EDIT_PERSON));
					saveButton.getAction().putValue("EditPersonDialog", this);
					saveButton.setText("Save");
					saveButton.setBounds(12, 224, 79, 32);
				}
			}
			this.setSize(302, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updatePersonList() {
		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = model.getSchedule().getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		personList.setModel(list2Model);
		personList.setCellRenderer(new ModelCellRenderer());
	}

	private void personListValueChanged(ListSelectionEvent evt) {
		if (personList.getSelectedValue() == null) {
			nameTextField.setText("");
			emailTextField.setText("");
			telephoneTextField.setText("");
		} else {
			Person temp;
			temp = model.getSchedule().getPerson(((PersonWrapper) personList
					.getSelectedValue()).getId());

			nameTextField.setText(temp.getName());
			emailTextField.setText(temp.getEmailAddress());
			telephoneTextField.setText(temp.getPhoneNumber());
		}
	}
	
	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return cancelAction;
	}
	
	public int getPersonId() {
		return ((PersonWrapper) personList
				.getSelectedValue()).getId();
	}
	
	public String getPersonName() {
		return nameTextField.getText();
	}
	
	public String getPersonEmail() {
		return emailTextField.getText();
	}
	
	public String getPersonPhone() {
		return telephoneTextField.getText();
	}
}
